<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0"><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />   
        <title>Welcome to Design Zone......</title>
		<link rel="stylesheet" type="text/css" href="../../scripts/ext/resources/css/ext-all.css" />
		<link rel="stylesheet" type="text/css" href="../../scripts/ext/resources/css/file-upload.css" />
        <link rel="stylesheet" type="text/css" href="../../styles/dzbase.css" />
        <script type="text/javascript" src="../../scripts/ext/adapter/ext-base.js">
        </script>
        <script type="text/javascript" src="../../scripts/ext/ext-all.js">
        </script>
        <script type="text/javascript" src="../../scripts/menu/EditableItem.js">
        </script>
        <script type="text/javascript" src="../../scripts/menu/RangeMenu.js">
        </script>
		<script type="text/javascript" src="../../scripts/grid/GridFilters.js">
		</script>
		<script type="text/javascript" src="../../scripts/grid/filter/Filter.js">
		</script>
		<script type="text/javascript"
			src="../../scripts/grid/filter/StringFilter.js">
		</script>
		<script type="text/javascript"
			src="../../scripts/grid/filter/ListFilter.js">
		</script>
		<script type="text/javascript"
			src="../../scripts/grid/filter/DateFilter.js"></script>
		<script type="text/javascript" src="../../scripts/grid/RowExpander.js">
		</script>
		<script type="text/javascript" src="../../scripts/form/FileUploadField.js">
        </script>
        <script type="text/javascript" src="../../scripts/i18nMessage.js">
        </script>
        <script type="text/javascript" src="../../scripts/selectMeasureSpecification.js">
        </script>
        <script type="text/javascript" src="../../scripts/utils.js">
        </script>
        <script type="text/javascript">
        	var msMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.measurespecification');
            var message = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=message');
			
			var statureXmlLoader = new xmlLoader('../../servlet/CodeTableServlet?name=CtStature');
			var collarXmlLoader = new xmlLoader('../../servlet/CodeTableServlet?name=CtCollarType');
			var customerXmlLoader = new xmlLoader('../../servlet/BiFranceCustomerServlet?action=5');
			var ctProductFamilyXmlLoader = new xmlLoader('../../servlet/CodeTableServlet?name=CtProductFamily');
			
			var statureStore;
			var collarStore;
			var customerStore;
			var ctProductFamilyStore;
			
			var statureCombo;
			var collarCombo;
			var customerCombo;
			
			var measureSpecForm;
			
			var setPatronage = function(){
				if(statureCombo.getValue() == null || statureCombo.getValue() == ''){
					return;
				}
				
				if(collarCombo.getValue() == null || collarCombo.getValue() == ''){
					return;
				}
				
				var cs = statureCombo.getRawValue();
				var cc = collarCombo.getRawValue();
				var maxPatronageIndextXmlLoader = new xmlLoader('../../servlet/BiMeasureSpecificationServlet?action=104&ctStature=' + statureCombo.getValue() + '&ctCollarType=' + collarCombo.getValue());
				var maxIndex = maxPatronageIndextXmlLoader.getValueByTagName('item');
				document.getElementById('patronageIndex').value = maxIndex;
				
				var patronage = statureCombo.getValue() + '-' + cc.substring(cc.length-1) + '-' + maxIndex;
				document.getElementById('patronage').value = patronage
				
			}

			var selectMsCallback = function(msId, patronage, photo1Name, photo2Name){
				var preId = Ext.getCmp('measureSpecificationId').getValue();
                Ext.getCmp('similarId').setValue(msId);
                measureSpecForm.getForm().load({
					url:'../../servlet/BiMeasureSpecificationServlet?action=3&id=' + msId, 
					waitMsg:Ext.LoadMask.prototype.msg, 
					success: function(form, action){
                		document.getElementById('customImg').src =  '../../_temp/' + photo1Name;
						document.getElementById('customImg1').src =  '../../_temp/' + photo2Name;
						Ext.getCmp('measureSpecificationId').setValue(preId);
						document.getElementById('similarId').value = msId;
						Ext.getCmp('sameAs').setValue(patronage);
						setPatronage();
					}
			});
            }
            
			Ext.onReady(function(){
                Ext.QuickTips.init();
                
                // turn on validation errors beside the field globally
                Ext.form.Field.prototype.msgTarget = 'side';
				
				var idValue = getPrarmeterValue(window.location, 'id');
                var startPage = getPrarmeterValue(window.location, 'startPage');
				
				statureStore = new Ext.data.Store({
                   
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'code',
                        totalRecords: 'itemTotal'
                    }, [
                    'code', 'description']),
					data: statureXmlLoader.getXml()
                });

				collarStore = new Ext.data.Store({
                    
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'code',
                        totalRecords: 'itemTotal'
                    }, [
                    'code', 'description']),
					data: collarXmlLoader.getXml()
                });
				
				customerStore = new Ext.data.Store({
	                reader: new Ext.data.XmlReader({
	                    record: 'item',
	                    id: 'id',
	                    totalRecords: 'itemTotal'
	                }, ['id', 'name', 'department']),
	                data: customerXmlLoader.getXml()
	            });		
				
				ctProductFamilyStore = new Ext.data.Store({
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'code',
                        totalRecords: 'itemTotal'
                    }, [ 
                    'code', 'description']),
					data: ctProductFamilyXmlLoader.getXml()
                });
				
				statureCombo = new Ext.form.ComboBox({
					fieldLabel: msMessage.get('ui.measurespecification.stature'),
					id: 'ctStature',
					name: 'ctStature',
					allowBlank: false,
                    blankText: message.get('message.edit.noblank'),
					editable: false,
					valueField: 'code',
				    store: statureStore,
				    displayField:'description',
				    triggerAction: 'all',
				    emptyText: message.get('message.combo.pleaseselect'),
				    selectOnFocus:true,
					mode: 'local'
				});
				
				collarCombo = new Ext.form.ComboBox({
					fieldLabel: message.get('message.edit.collar'),
					id: 'ctCollarType',
					name: 'ctCollarType',
					allowBlank: false,
                    blankText: message.get('message.edit.noblank'),
					editable: false,
				    store: collarStore,
					valueField: 'code',
				    displayField:'description',
				    triggerAction: 'all',
				    emptyText: message.get('message.combo.pleaseselect'),
				    selectOnFocus:true,
					mode: 'local'
				});
				
				customerCombo = new Ext.form.ComboBox({
                    fieldLabel: message.get('message.edit.customername'),
                    id: 'customerId',
                    name: 'customerId',
                    tpl: '<tpl for="."><div class="x-combo-list-item">{name}&nbsp;&nbsp;--&nbsp;&nbsp;{department}</div></tpl>',
                    valueField: 'id',
                    typeAhead: true,
                    store: customerStore,
                    displayField: 'name',
                    forceSelection: true,
                    triggerAction: 'all',
                    emptyText: message.get('message.combo.pleaseselect'),
                    selectOnFocus: true,
                    mode: 'local'
                });
				
				var ctProductFamilyCombo = new Ext.form.ComboBox({
					fieldLabel: message.get('message.edit.productfamily'),
					id: 'ctProductFamily',
					name: 'ctProductFamily',
					allowBlank: false,
                    blankText: message.get('message.edit.noblank'),
					editable: false,
				    store: ctProductFamilyStore,
					valueField: 'code',
				    displayField:'description',
				    triggerAction: 'all',
				    emptyText: message.get('message.combo.pleaseselect'),
				    selectOnFocus:true,
					mode: 'local'
				});
				
				measureSpecForm = new Ext.FormPanel({
					fileUpload: true,
                    labelAlign: 'top',
                    labelWidth: 75, // label settings here cascade unless overridden
                    url: '../../servlet/BiMeasureSpecificationServlet?action=1',
                    frame: true,
                    reader: new Ext.data.XmlReader({
                        record: 'item'
                    }, [{
                        name: 'id',
                        mapping: 'id'
                    }, {
                        name: 'name',
                        mapping: 'name'
                    }, {
                        name: 'ctStature',
                        mapping: 'ctStature'
                    }, {
                        name: 'ctCollarType',
                        mapping: 'ctCollarType'
                    }, {
                        name: 'realPhoto1Name',
                        mapping: 'realPhoto1Name'
                    }, {
                        name: 'realPhoto2Name',
                        mapping: 'realPhoto2Name'
                    }, {
                        name: 'ctProductFamily',
                        mapping: 'ctProductFamily'
                    }, {
                        name: 'patronageIndex',
                        mapping: 'patronageIndex'
                    }, {
                        name: 'patronage',
                        mapping: 'patronage'
                    }, {
                        name: 'similarId',
                        mapping: 'similarId'
                    }, {
                        name: 'customerReference',
                        mapping: 'customerReference'
                    }]),
                    title: msMessage.get('ui.measurespecification.edittitle'),
                    bodyStyle: 'padding:5px 5px 5px 5px',
                    defaults: {
                        width: '90%'
                    },
                    defaultType: 'textfield',
                    
                    items: [{
                        id: 'measureSpecificationId',
                        name: 'id',
                        xtype: 'hidden'
                    },{
                        id: 'patronageIndex',
                        name: 'patronageIndex',
                        xtype: 'hidden'
                    },{
                        id: 'similarId',
                        name: 'similarId',
                        xtype: 'hidden'
                    }, 
//					{
//                        fieldLabel: msMessage.get('ui.measurespecification.name'),
//                        name: 'name',
//                        allowBlank: false,
//                        blankText: message.get('message.edit.noblank'),
//                        maxLength: 64,
//                        maxLengthText: message.get('message.edit.toolong')
//                    }, 
					//ctProductFamilyCombo,
					statureCombo, collarCombo,
					{
						fieldLabel: message.get('message.edit.customerreference'),
						id: 'customerReference',
                        name: 'customerReference',
                        maxLength: 128,
                        maxLengthText: message.get('message.edit.toolong')
					},
					//customerCombo, 
					{
                        fieldLabel: msMessage.get('ui.measurespecification.sameas'),
						id: 'sameAs',
                        name: 'sameAs',
                        emptyText: message.get('message.combo.pleaseselect'),
                        listeners: {
                            'render': {
                                fn: function(){
                                    Ext.getCmp('sameAs').getEl().on('click', function(){
                                        if(statureCombo.getValue() == ''){
                                        	messageTip.msg(message.get('message.tip'), msMessage.get('ui.measurespecification.stature') + '. ' + message.get('message.edit.noblank'));
												return;
                                         }
                                        if(collarCombo.getValue() == ''){
                                        	messageTip.msg(message.get('message.tip'), message.get('message.edit.collar') + '. ' + message.get('message.edit.noblank'));
												return;
	                                     }
                                    	selectMs(selectMsCallback, statureCombo.getValue(), collarCombo.getValue());
                                    }, this);
                                },
                                scope: this
                            }
                        }
                    },{
                        fieldLabel: msMessage.get('ui.measurespecification.patronage'),
						id: 'patronage',
                        name: 'patronage',
                        allowBlank: false,
                        blankText: message.get('message.edit.noblank'),
                        maxLength: 64,
                        maxLengthText: message.get('message.edit.toolong'),
                        listeners: {
                            'render': {
                                fn: function(){
                                    Ext.getCmp('patronage').getEl().on('click', function(){
                                        setPatronage();
                                    }, this);
                                },
                                scope: this
                            }
                        }
                    },{
						xtype: 'fileuploadfield',
						width: '80%',
			            id: 'realPhoto1',
			            emptyText: message.get('message.edit.selectimage'),
			            fieldLabel: msMessage.get('ui.measurespecification.photo'),
			            name: 'realPhoto1',
			            buttonCfg: {
			                text: '',
			                iconCls: 'uploadIcon'
			            }
					}, {
						xtype: 'fileuploadfield',
						width: '80%',
			            id: 'realPhoto2',
			            emptyText: message.get('message.edit.selectimage'),
			            fieldLabel: msMessage.get('ui.measurespecification.photo'),
			            name: 'realPhoto2',
			            buttonCfg: {
			                text: '',
			                iconCls: 'uploadIcon'
			            }
					}, {
						id: 'realPhoto1Name',
						name: 'realPhoto1Name',
						xtype: 'hidden'
					}, {
						id: 'realPhoto2Name',
						name: 'realPhoto2Name',
						xtype: 'hidden'
					}],
                    
                    buttons: [{
                        text: message.get('message.edit.save'),
                        handler: function(){
                            if (measureSpecForm.getForm().isValid()) {
                                //messageForm.hide();
                                if(idValue == null){
                                	setPatronage();
                                }
								document.getElementsByName('ctCollarType')[0].value = collarCombo.getValue();
								document.getElementsByName('ctStature')[0].value = statureCombo.getValue();
								//document.getElementsByName('ctProductFamily')[0].value = ctProductFamilyCombo.getValue();
								//document.getElementsByName('customerId')[0].value = customerCombo.getValue();
								
                                measureSpecForm.getForm().submit({
                                    method: 'post',
                                    success: function(form, action){
                                        //alert(form);
                                        messageTip.msg(message.get('message.tip'), message.get('message.edit.successful'));
										document.getElementById('realPhoto1-file').value = null;
										document.getElementById('realPhoto2-file').value = null;
                                        measureSpecForm.getForm().reset();
                                        //messageForm.show();
                                    },
                                    failure: function(form, action){
                                        //alert(form);
                                        messageTip.msg(message.get('message.tip'), message.get('message.edit.unsuccessful'));
                                        //messageForm.show();
                                    }
                                });
                            }
                        }
                    },{
						text: message.get('message.edit.reset'),
						handler: function(){
							measureSpecForm.getForm().reset();
						}
					}, {
                        text: message.get('message.edit.cancel'),
                        handler: function(){
                            if (startPage != null) {
                                window.location = 'measure_specification_list.html?startPage=' + startPage;
                            }else {
                                window.location = 'measure_specification_list.html';
                            }
                        }
                    }]
                }); 
				

				measureSpecForm.render('measureSpec');
				
				new Ext.Resizable('customImg', {
		            wrap:true,
		            pinned:true,
					dynamic:true,
				    handles: 'all',
				    draggable:true
				});
				
				new Ext.Resizable('customImg1', {
		            wrap:true,
		            pinned:true,
					dynamic:true,
				    handles: 'all',
				    draggable:true
				});
				
				if(idValue != null){
					document.getElementById('measureSpecificationId').value = idValue;
					measureSpecForm.getForm().load({
							url:'../../servlet/BiMeasureSpecificationServlet?action=3&id=' + idValue, 
							waitMsg:Ext.LoadMask.prototype.msg, 
							success: function(form, action){
								document.getElementById('customImg').src =  '../../_temp/' + document.getElementById('realPhoto1Name').value;
								document.getElementById('customImg1').src =  '../../_temp/' + document.getElementById('realPhoto2Name').value;
							}
					});
				}
			});
        
        </script>
    </head>
    <body>
        <div id="measureSpec">
        </div>
		<img id="customImg" src="../../_temp/noimage.gif" style="position:absolute;left: 20%;width: 250px;"/>
		<img id="customImg1" src="../../_temp/noimage.gif" style="position:absolute;left: 60%;width: 250px;"/>
    </body>
</html>
